package zgrjb.snbq.teach_platform.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import zgrjb.snbq.teach_platform.util.JsonData;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/api/v1/upload")
@Slf4j
public class UploadController {

    private static final String UPLOAD_DIR = "uploads/";
    private static final long MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB

    @PostMapping("/file")
    public JsonData uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            // 验证文件
            if (file.isEmpty()) {
                return JsonData.buildError("文件不能为空");
            }

            if (file.getSize() > MAX_FILE_SIZE) {
                return JsonData.buildError("文件大小不能超过10MB");
            }

            // 创建上传目录
            File uploadDir = new File(UPLOAD_DIR);
            if (!uploadDir.exists()) {
                uploadDir.mkdirs();
            }

            // 生成唯一文件名
            String originalFilename = file.getOriginalFilename();
            String extension = "";
            if (originalFilename != null && originalFilename.contains(".")) {
                extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            }
            String filename = UUID.randomUUID().toString() + extension;

            // 保存文件
            Path filePath = Paths.get(UPLOAD_DIR + filename);
            Files.copy(file.getInputStream(), filePath);

            // 返回文件信息
            Map<String, Object> fileInfo = new HashMap<>();
            fileInfo.put("filename", filename);
            fileInfo.put("originalName", originalFilename);
            fileInfo.put("size", file.getSize());
            fileInfo.put("contentType", file.getContentType());
            fileInfo.put("url", "/api/v1/upload/file/" + filename);

            log.info("文件上传成功: {}", filename);
            return JsonData.buildSuccess(fileInfo);

        } catch (IOException e) {
            log.error("文件上传失败: {}", e.getMessage());
            return JsonData.buildError("文件上传失败: " + e.getMessage());
        }
    }

    @GetMapping("/file/{filename}")
    public JsonData getFileInfo(@PathVariable String filename) {
        try {
            Path filePath = Paths.get(UPLOAD_DIR + filename);
            if (!Files.exists(filePath)) {
                return JsonData.buildError("文件不存在");
            }

            Map<String, Object> fileInfo = new HashMap<>();
            fileInfo.put("filename", filename);
            fileInfo.put("size", Files.size(filePath));
            fileInfo.put("url", "/api/v1/upload/file/" + filename);

            return JsonData.buildSuccess(fileInfo);

        } catch (IOException e) {
            log.error("获取文件信息失败: {}", e.getMessage());
            return JsonData.buildError("获取文件信息失败");
        }
    }
} 